iOS Swift判断代码运行在模拟器上

iOS模拟器有些功能没有,比如拍照,因此代码中需要加个模拟器判断,查了好多文章,终于找到了。swift代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Platform {
static let isSimulator: Bool = {
var isSim = false
#if arch(i386) || arch(x86_64)
isSim = true
#endif
return isSim
}()
}

// Elsewhere...

if Platform.isSimulator {
// Do one thing
}
else {
// Do the other
}

参考 http://themainthread.com/blog/2015/06/simulator-check-in-swift.html

Contents
,